<!--
 * @Description: 返单
 * 
 * @Author: Edan<Edan>
 * @Date: 2024-08-06 15:37:13
 * @FilePath: /yuxiuproweb/src/views/qb/order/components/returnOrder.vue
-->
<template>
  <div class="">
    <div style="display: flex; align-items: center">
      <el-button
        type="success"
        size="mini"
        class="ml20"
        icon="el-icon-download"
        @click="handleDownload"
        >生成图片</el-button
      >
    </div>
    <div class="split-view" id="printPDF">
      <el-descriptions class="margin-top split-descriptions" :column="4" border>
        <el-descriptions-item>订单号：{{ form.orderId }} </el-descriptions-item>
        <el-descriptions-item>
          订单类型：{{ dict.label.qb_order_state[form.orderType] }}
        </el-descriptions-item>
        <el-descriptions-item>
          订单日期：{{ parseTime(form.createTime, "{y}-{m}-{d}") }}
        </el-descriptions-item>
        <el-descriptions-item>
          发运方式：{{ form.deliveryMethod }}
        </el-descriptions-item>

        <el-descriptions-item>
          客户名称：{{ form.clientName }}
        </el-descriptions-item>

        <el-descriptions-item>
          联系电话：{{ form.telnum }}
        </el-descriptions-item>
        <el-descriptions-item>
          收货地址：{{ form.deliveryAddress }}
        </el-descriptions-item>

        <el-descriptions-item>
          物流付款方式：{{
            form.postageModeOfPayment == 0
              ? "寄付"
              : form.postageModeOfPayment == 1
              ? "到付"
              : "包邮"
          }}
        </el-descriptions-item>
        <el-descriptions-item>
          联系人：{{ form.consignee }}
        </el-descriptions-item>
        <!-- <el-descriptions-item>
          账户金额
          <span>现金：{{ clientRow.attr2 ? clientRow.attr2 : 0 }}</span>
        </el-descriptions-item> -->
        <el-descriptions-item :span="3">
          <span v-if="form.totalPrice" style="margin-right: 8px;color:red;"
            >订单总计:￥{{ form.totalPrice }};</span
          >
          <span v-if="form.orderType == 'SA' || form.orderType == 'SO'" style="display: flex">
            <span style="margin-right: 8px">本单金额:￥{{ form.price }};</span>
            <span style="margin-right: 8px">包装:￥{{ form.packingFee }};</span>
            <span style="margin-right: 8px"
              >快递:￥{{ form.logisticsFee }};</span
            >
            <span style="margin-right: 8px">零裁:￥{{ form.zeroCutFee }};</span>
          </span>
        </el-descriptions-item>
        <el-descriptions-item>
          备注信息：{{ form.notes }}
        </el-descriptions-item>
      </el-descriptions>
      <!--         :max-height="maxHeight" -->
      <el-table
        ref="tableItem"
        v-loading="false"
        :data="list"
        border
        show-summary
        :summary-method="getSummaries"
        style="margin-top: 10px; felx: 1; width: 100%"
      >
        <template v-for="(i, index) in tableForm.item">
          <el-table-column
            :key="index"
            :prop="i.itemcode"
            :columnKey="i.itemcode"
            :label="i.itemname"
            :align="i.aligntype ? i.aligntype : 'center'"
            :min-width="i.minwidth"
            :width="!!i.width ? i.width : ''"
            :show-overflow-tooltip="i.overflow ? true : false"
            :sortable="i.sortable ? true : false"
            :fixed="i.fixed"
            v-if="i.displaymark ? true : false"
          >
            <template slot-scope="scope">
              <div v-if="i.itemcode == 'technology'">
                {{ scope.row[i.itemcode] | technologyFormat }}
              </div>
              <div v-else-if="i.itemcode == 'height'">
                <span v-if="scope.row.unit == '平方米'">{{
                  scope.row[i.itemcode]
                }}</span>
                <span
                  v-else-if="
                    scope.row.unit == '幅' && scope.row.fabricType == '2'
                  "
                  >{{ scope.row[i.itemcode] }}</span
                >
                <span v-else>-</span>
              </div>
              <span v-else>{{ scope.row[i.itemcode] }}</span>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>
  </div>
</template>

<script>
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
import commonMix from "@/utils/mixins/common";
import { tableItem } from "./tablecolums.js";
export default {
  mixins: [commonMix],
  components: {},
  props: {},
  dicts: ["qb_order_state"],
  data() {
    return {
      form: {},
      list: [],
      tableForm: tableItem,
    };
  },
  mounted() {},
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      var arr = ["length", "subtotal"];
      const sums = [];
      columns.forEach((column, index) => {
        var bor = false;
        if (
          arr.length > 0 &&
          arr.find((item) => item == column.property) != undefined
        ) {
          bor = true;
        }
        if (index === 0) {
          sums[index] = "合计";
        } else {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value)) && bor) {
            sums[index] = Number(
              values
                .reduce((prev, curr) => {
                  const value = Number(curr);
                  if (!isNaN(value)) {
                    return prev + curr;
                  } else {
                    return prev;
                  }
                }, 0)
                .toFixed(2)
            );
          }
        }
      });
      return sums;
    },
    handleDownload() {
      const cloneDom = document.getElementById("printPDF").cloneNode(true);
      document.getElementsByTagName("body")[0].appendChild(cloneDom);
      html2Canvas(cloneDom, { allowTaint: true }).then((canvas) => {
        let contentWidth = canvas.width;
        let contentHeight = canvas.height;
        let pageHeight = (contentWidth / 592.28) * 841.89;
        let leftHeight = contentHeight;
        let position = 0;
        let imgWidth = 595.28;
        let imgHeight = (592.28 / contentWidth) * contentHeight;
        let pageData = canvas.toDataURL("image/jpeg", 1.0);
        // console.log(pageData);

        var oA = document.createElement("a");
        oA.download = this.form.orderId; // 设置下载的文件名，默认是'下载'
        oA.href = pageData;
        document.body.appendChild(oA);
        oA.click();
        oA.remove();
        document.getElementsByTagName("body")[0].removeChild(cloneDom);
      });
    },
    countPriceFun(row) {
      row.pricetotal = 0;
      if (!!row.price) {
        row.pricetotal = Number(
          (
            (Number(row.price) + (row.technology == 3 ? 8 : 0)) *
            Number(row.length) *
            Number(row.height)
          ).toFixed(2)
        );
      }
      return row.pricetotal;
    },
  },
  filters: {
    technologyFormat(data) {
      var str = "";
      switch (data) {
        case 0:
          str = "冷胶";
          break;
        case 1:
          str = "热胶";
          break;
        case 2:
          str = "无工艺";
          break;
        case 3:
          str = "先冷后热";
          break;
        default:
          break;
      }
      return str;
    },
    dateFormat(dataStr) {
      if (!dataStr) {
        return;
      }
      var dt = new Date(dataStr);
      var y = dt.getFullYear();
      var m = (dt.getMonth() + 1).toString().padStart(2, "0");
      var d = dt.getDate().toString().padStart(2, "0");
      var hh = dt.getHours().toString().padStart(2, "0");
      var mm = dt.getMinutes().toString().padStart(2, "0");
      var ss = dt.getSeconds().toString().padStart(2, "0");
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
  },
};
</script>

<style scoped lang="scss">
.split-view {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  .split-descriptions {
    font-size: 16px;
    font-weight: bold;
    ::v-deep {
      th {
        display: none;
      }
      .el-descriptions-item__cell {
        border: 1px solid rgb(50, 50, 50);
        color: rgb(96, 98, 102);
        font-size: 16px;
        font-weight: bold;
      }
    }
  }
  .drawer-collapse {
    ::v-deep {
      .el-collapse-item__header {
        height: 65px;
        line-height: 65px;
        font-size: 16px;
      }
    }
  }
}
::v-deep {
  .el-table--border,
  .el-table--group {
    border: 1px solid #000 !important;
  }
  /**
改变表格内竖线颜色
 */
  .el-table--border td,
  .el-table--border th,
  .el-table__body-wrapper
    .el-table--border.is-scrolling-left
    ~ .el-table__fixed {
    border-right: 1px solid #000 !important;
  }
  /**
改变表格内行线颜色
 */
  .el-table td,
  .el-table th.is-leaf,
  .el-table__cell {
    border-bottom: 1px solid #000 !important;
  }
}
</style>
